/*
 * @(#)JVM.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.jmx;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.util.HashMap;
import java.util.Map;

import javax.management.NotCompliantMBeanException;
import javax.management.StandardMBean;

/**
 * @author Web Platform Development Team
 */
public class JVM extends StandardMBean implements JVMMBean {
    String name;
    Map<String, String> jvmStatusMap = new HashMap<String, String>(2);

    /**
     * Instantiates a new jVM.
     *
     * @param name     the server name
     * @param mbeanInterface the mbean interface
     * @throws NotCompliantMBeanException the not compliant m bean exception
     */
    public JVM(String name, Class<?> mbeanInterface) throws NotCompliantMBeanException {
        super(JVMMBean.class);
        this.name = name;
    }

    /**
     * Gets the java version.
     *
     * @return the java version
     * @throws Exception the exception
     */
    public String getjavaVersion() throws Exception {
        return java.lang.System.getProperty("java.vm.version");
    }

    /**
     * Gets the java vendor.
     *
     * @return the java vendor
     * @throws Exception the exception
     */
    public String getjavaVendor() throws Exception {
        return java.lang.System.getProperty("java.vm.vendor");
    }

    /**
     * Gets the oS name.
     *
     * @return the oS name
     * @throws Exception the exception
     */
    public String getOSName() throws Exception {
        return java.lang.System.getProperty("os.name");
    }

    /**
     * Gets the oS version.
     *
     * @return the oS version
     * @throws Exception the exception
     */
    public String getOSVersion() throws Exception {
        return java.lang.System.getProperty("os.version");
    }

    /**
     * Gets the active thread count.
     *
     * @return the active thread count
     * @throws Exception the exception
     */
    public int getActiveThreadCount() throws Exception {
        return ManagementFactory.getThreadMXBean().getThreadCount();
    }

    /**
     * Gets the used memory.
     *
     * @return the used memory
     */
    public long getUsedMemory() {
        return getCurrentMemory();
    }

    /**
     * Gets the total memory.
     *
     * @return the total memory
     */
    public long getTotalMemory() {
        return Runtime.getRuntime().totalMemory();
    }

    /**
     * @see com.nhncorp.usf.core.jmx.JVMMBean#getName()
     * {@inheritDoc}
     */
    public String getName() {
        return name;
    }

    /**
     * Gets the current memory.
     *
     * @return the current memory
     */
    private long getCurrentMemory() {
        MemoryMXBean memoryMBean = ManagementFactory.getMemoryMXBean();
        return memoryMBean.getHeapMemoryUsage().getUsed() + memoryMBean.getNonHeapMemoryUsage().getUsed();
    }

    /**
     *  Gets the monitoring data
     *  
     * @return the usf server monitoring data
     */
    public Map<String, String> stat() {
        try {
            jvmStatusMap.put(THREAD_ITEM_KEY, String.valueOf(getActiveThreadCount()));
        } catch (Exception e) {
            jvmStatusMap.put(THREAD_ITEM_KEY, "0");
        }
        jvmStatusMap.put(MEMORY_ITEM_KEY, String.valueOf(getUsedMemory()));
        return jvmStatusMap;
    }
}
